import pygame, random, sys
from pygame.locals import *

import functions
import collections

MAXRESOURCES = 50000
MAXLARGEITEMS = 256
MAXSMALLITEMS = 512
RETIREMENT = 65
YEAR = 365
CAPTAIN = 82
ADMIRAL = 270

RED = (255, 0, 0)
YELLOW = (204, 204, 0)
GREEN = (0, 255, 0)


class animate_this:
	def __init__(self, screen, image, w, h, x, y, framedelay=20, playspeed=1):
		self.x = x
		self.y = y
		self.screen = screen
		self.firstreverse = False
		self.stillplaying = False

		self.master_image = image.convert_alpha()
		self.master_width, self.master_height = self.master_image.get_size()

		self.playspeed = playspeed
		self.framedelay = framedelay
		self.timepassed = 0

		self.img = []

		for i in xrange(int(self.master_width/w)):
			self.img.append(self.master_image.subsurface((i*w,0,w,h)))

		self.ani_max = len(self.img)-1
		self.ani_pos = 0

	def update(self, timedelay, loop=True):
		self.timepassed += timedelay * self.playspeed
		if (self.ani_pos == 0):
			self.stillplaying = True
		if (int(self.timepassed) >= int(self.framedelay)):
			self.timepassed = self.timepassed - self.framedelay
			if (self.timepassed < 0):
				self.timepassed = 0
			if self.ani_pos < self.ani_max:
				self.ani_pos += 1
			else:
				if (loop):
					self.ani_pos = 0
				else:
					self.stillplaying = False

		tempSurf = self.img[self.ani_pos]
		self.screen.blit(tempSurf,(self.x, self.y))

	def reverse(self, timedelay, loop=True):
		if not (self.firstreverse):
			self.ani_pos = self.ani_max
			self.firstreverse = True
		if (self.ani_pos == self.ani_max):
			self.stillplaying = True
		self.timepassed += timedelay * self.playspeed
		if (int(self.timepassed) >= int(self.framedelay)):
			self.timepassed = self.timepassed - self.framedelay
			if (self.timepassed < 0):
				self.timepassed = 0
			if self.ani_pos > 0:
				self.ani_pos -= 1
			else:
				if (loop):
					self.ani_pos = self.ani_max
				else:
					self.stillplaying = False

		tempSurf = self.img[self.ani_pos]
		self.screen.blit(tempSurf,(self.x, self.y))

	def stop(self):
		self.stillplaying = False
		tempSurf = self.img[self.ani_pos]
		self.screen.blit(tempSurf,(self.x, self.y))

	def reset(self):
		self.stillplaying = False
		self.ani_pos = 0

	def setxy(self, x, y):
		self.x = x
		self.y = y

	def StillPlaying(self):
		return(self.stillplaying)


class which_music:
	def __init__(self, defaultfile):
		self.currentMusic = defaultfile
		self.newFile = False
		pygame.mixer.music.load(self.currentMusic)
		pygame.mixer.music.stop()
#		pygame.mixer.music.play(-1, 0.0)
	
	def update(self,newfile):
		self.newfile = newfile
		if (self.newfile != self.currentMusic):
			self.currentMusic = self.newfile
			pygame.mixer.music.stop()
			pygame.mixer.music.load(self.currentMusic)
#			pygame.mixer.music.play(-1, 0.0)

	def stop(self):	
		self.currentMusic = False
		pygame.mixer.music.stop()


class mouseOver:
	def __init__(self, SCALEGFX):
		self.name = False
		self.prevMouseCollideRect = False
		self.mouseOver = False
		self.description = False
		self.typeOfimage = False
		self.BlankPageDict = functions.scaled_image_dict('', '', '', 'gfx/BlankPage.png', SCALEGFX, 131, 52)

	def mouseOverDetect(self,MouseIconRect, mouseOverSound, interactiveIcons, iteminfo, screen):
		self.DisplayDict = False
		self.mouseCollision, self.mouseCollideRect, self.CollideName, self.typeOfimage, self.description = functions.mouseOverIcon(MouseIconRect, interactiveIcons)
		if (self.mouseCollision):
			if (self.prevMouseCollideRect != self.mouseCollideRect):
				mouseOverSound.play()
				self.prevMouseCollideRect = self.mouseCollideRect
				self.mouseOver = True
			self.name = self.CollideName
		else:
			self.prevMouseCollideRect = False
			self.mouseOver = False
			self.description = False
			self.typeOfimage = False
		if (self.mouseOver) and (self.typeOfimage == 'build'):
			self.DisplayDict = iteminfo.GetBookItemSurfaces(self.name)
			screen.blit(self.BlankPageDict['surface'], self.BlankPageDict['rect'])
			screen.blit(self.DisplayDict['surface'], self.DisplayDict['rect'])
		elif (self.mouseOver) and (self.typeOfimage == 'research'):
			self.DisplayDict = iteminfo.GetResearchItemSurfaces(self.name)
			screen.blit(self.DisplayDict['surface'], self.DisplayDict['rect'])
		return (self.mouseOver, self.mouseCollideRect, self.name, self.typeOfimage, self.description)


class toplefttext:
	def __init__(self, screen, scalegfx):
		self.screen = screen
		self.scalegfx = scalegfx
		self.font = pygame.font.SysFont(None, 12 * self.scalegfx)
		self.textcolour = (255, 255, 255)
		self.message = 'Welcome to pyDeuteros'
		self.firstmsg = True

	def update(self, message):
		if (message and self.firstmsg):
			self.message = message
			self.firstmsg = False
		if not (self.firstmsg):
			self.message = message
		functions.drawText(self.message, self.font, self.textcolour, self.screen, (5*self.scalegfx), (5*self.scalegfx))


class solsystemtext:
	def __init__(self, screen, scalegfx):
		self.screen = screen
		self.scalegfx = scalegfx
		self.font = pygame.font.SysFont(None, 12 * self.scalegfx)
		self.textcolour = (255, 255, 0)

	def update(self, solsystemname):
		functions.drawText(solsystemname, self.font, self.textcolour, self.screen, (260*self.scalegfx), (5*self.scalegfx))

class dialogBox:
	def __init__(self, screen, scalegfx):
		self.screen = screen
		self.scalegfx = scalegfx
		self.display = False
		self.message = False
		self.ButtonDict = functions.scaled_image_dict('ok', 'Dismiss', 'ok', 'gfx/BlankButton.png', self.scalegfx, 168, 128)

	def setDialog(self, message):
		self.display = True
		self.message = message

	def showDialog(self):
		if (self.display):
			functions.DialogBox(self.message, self.screen, self.scalegfx, self.ButtonDict)

	def getIcon(self):
		if (self.display):
			return(self.ButtonDict)
		else:
			return(False)

	def unsetDialog(self):
		self.display = False
		self.message = False


class crewnames:
	def __init__(self, filelist):
		self.name = False
		self.filelist = filelist
		self.names = []
		self.loadnames()

	def loadnames(self):
		del self.names [:]
		with open(self.filelist) as self.f:
			self.names = self.f.read().splitlines()
		random.shuffle(self.names)

	def getname(self):
		if (len(self.names) <= 0):
			self.loadnames()
		self.name = str(self.names.pop())
		return(self.name)


class crew:
	def __init__(self, leadername, crewtype, numberofcrew, trainingtime):
		self.leadername = leadername
		self.crewtype = crewtype
		self.numberofcrew = numberofcrew
		self.location = False
		self.sublocation = False
		self.numberoftasks = 0
		self.rank = False
		self.remainingtrainingtime = trainingtime
		self.crewToAdd = 0
		self.averageAge = 9125
		self.inCryo = False

		if (crewtype == 'pilot'):
			self.rank = 'Pilot'
		elif (crewtype == 'engineer'):
			self.rank = 'Apprentice'
		elif (crewtype == 'researcher'):
			self.rank = 'Technician'

	def dotraining(self):
		self.remainingtrainingtime -= 1
		if (self.remainingtrainingtime <= 0):
			self.remainingtrainingtime = 0
			if (self.crewToAdd > 0):
				self.numberofcrew += self.crewToAdd
				self.crewToAdd = 0
		return(self.remainingtrainingtime)

	def dotick(self):
		if not (self.inCryo):
			self.averageAge += 1

	def taskdone(self, modifier = 0):
		self.numberoftasks += 1 + modifier
		if (self.crewtype == 'pilot') and not (self.rank == 'Admiral') and not (self.rank == 'Warlord'):
			if (self.numberoftasks > ADMIRAL):
				self.rank = 'Admiral'
			elif (self.numberoftasks > CAPTAIN):
				self.rank = 'Captain'

	def killsomecrew(self):
		self.dead = False
		if not (self.inCryo):
			if (random.randint(0, 365) == random.randint(0, 365)):
				self.numberofcrew -= 1
			if (self.numberofcrew <= 0) or ((self.averageAge / YEAR) > RETIREMENT):
				self.dead = True
		return(self.dead)

	def getdetails(self):
		return(self.leadername, self.crewtype, self.numberofcrew, self.location, self.sublocation, self.rank)

	def doTopUp(self, addcrew, trainingtime):
		self.crewToAdd = addcrew
		self.remainingtrainingtime = trainingtime

	def setLocation(self, location, sublocation):
		self.location = location
		self.sublocation = sublocation

	def inCryo(self):
		self.inCryo = True

	def leaveCryo(self):
		self.inCryo = False

	def setRank(self, rankname):
		self.rank = rankname


class theTicker:
	def __init__(self, screen, scalegfx):
		self.screen = screen
		self.scalegfx = scalegfx
		self.font = pygame.font.SysFont(None, 12 * self.scalegfx)
		self.textcolour = (255, 255, 255)
		self.year = 3100
		self.tick = 0.0
		self.msg = False

	def update(self, year, tick):
		if (year):
			self.year = year
		if (tick):
			self.tick = tick
		self.msg = '%(Year)04d %(Tick)06.2f' % {"Year": self.year, "Tick": self.tick}
		functions.drawText(self.msg, self.font, self.textcolour, self.screen, (80*self.scalegfx), (225*self.scalegfx))


class Messages:
	def __init__(self):
		self.messages = []
		self.UnRead = False
		self.Alert = False

	def addMsg(self, year, tick, msg):
		self.UnRead = True
		self.message = False
		self.message = '%(Year)04d %(Tick)06.2f: %(Message)s' % {"Year": year, "Tick": tick, "Message": msg}
		self.messages.append(self.message)

	def getMsgs(self):
		self.UnRead = False
		return(self.messages)

	def Unread(self):
		return(self.Unread)

	def SetAltert(self):
		self.Alert = True

	def UnSetAltert(self):
		self.Alert = False

	def Alerts(self):
		return(self.Alert)


class LocationInfo:
	def __init__(self, locationfile):
		self.locationinfo = functions.generic_load(locationfile)

	def GetResources(self, location):
		self.resources = []
		for self.location in self.locationinfo:
			if (self.location['id'] == location):
				self.resources = self.location['resources']
				break
		return(self.resources)

	def GetSolSystemName(self, location):
		self.longname = False
		self.shortname = False
		for self.location in self.locationinfo:
			if (self.location['id'] == location):
				self.shortname = self.location['solar_system']
				break
		if (self.shortname):
			for self.location in self.locationinfo:
				if (self.location['id'] == self.shortname):
					self.longname = self.location['long_name']
					break
		return(self.shortname, self.longname)


class ItemInfo:
	def __init__(self, itemfile, scalegfx):
		self.bookitemsurfacedicts = []
		self.progressitemsurfacedicts = {}
		self.researchitemsurfaces = {}
		self.matsDict = collections.OrderedDict()
		self.iteminfo = functions.generic_load(itemfile)

		for self.item in self.iteminfo:
			for self.key in self.item:
				if (self.key == 'image'):
					self.x, self.y = self.item['coords'].split(',', 1)
					self.bookitemsurfacedicts.append(functions.scaled_image_dict(self.item['id'], self.item['description'], '', self.item['image'], scalegfx, int(self.x), int(self.y)))
					break
			for self.key in self.item:
				if (self.key == 'progress'):
					self.progressitemsurfacedicts[self.item['id']] = []
					for self.image in self.item['progress']:
						self.progressitemsurfacedicts[self.item['id']].append(functions.scaled_image_dict('', '', '', self.image, scalegfx, 60, 52))
					break
			for self.key in self.item:
				if (self.key == 'rimage'):
					self.researchitemsurfaces[self.item['id']] = functions.scaled_image_dict('', '', '', self.item['rimage'], scalegfx, 208, 82)
					break

	def OrbitOnly(self, item):
		self.orbitonly = False
		for self.item in self.iteminfo:
			if (self.item['id'] == item):
				if (self.item['build'] == 'orbital'):
					self.orbitonly = True
				break
		return(self.orbitonly)

	def GetBookItemSurfaces(self, item):
		for self.surfacedict in self.bookitemsurfacedicts:
			if (self.surfacedict['name'] == item):
				break
		return(self.surfacedict)

	def GetResearchItemSurfaces(self, item):
		return(self.researchitemsurfaces[item])

	def GetItemName(self, item):
		self.name = False
		for self.item in self.iteminfo:
			if (self.item['id'] == item):
				self.name = self.item['long_name']
				break
		return(self.name)

	def GetItemDescription(self, item):
		self.desciption = False
		for self.item in self.iteminfo:
			if (self.item['id'] == item):
				self.description = self.item['description']
				break
		return(self.description)

	def GetItemTechLevel(self, item):
		self.techlevel = False
		for self.item in self.iteminfo:
			if (self.item['id'] == item):
				self.techlevel = self.item['tech']
				break
		return(self.techlevel)

	def GetItemMass(self, item):
		self.mass = 0
		for self.item in self.iteminfo:
			if (self.item['id'] == item):
				self.mass = int(self.item['mass'])
				break
		return(self.mass)

	def GetProgessSurfaceList(self, item):
		self.surfacelist = []
		del self.surfacelist [:]
		for self.key in self.progressitemsurfacedicts:
			if (self.key == item):
				self.surfacelist = list(self.progressitemsurfacedicts[self.key])
				break
		return(self.surfacelist)

	def GetBuildTime(self, item):
		self.time = False
		for self.item in self.iteminfo:
			if (self.item['id'] == item):
				self.time = int(self.item['aoc_build_time'])
				break
		return(self.time)

	def GetResearchTime(self, item):
		self.time = False
		for self.item in self.iteminfo:
			if (self.item['id'] == item):
				self.time = int(self.item['research_time'])
				break
		return(self.time)

	def GetRawMaterials(self, item):
		self.matsDict.clear()
		for self.item in self.iteminfo:
			if (self.item['id'] == item):
				for self.mats in self.item['materials']:
					self.material, self.amount = self.mats.split(':', 1)
					self.matsDict[self.material] = self.amount
				break
		return(self.matsDict)

	def ItemBuildable(self, item):
		self.buildable = False
		for self.item in self.iteminfo:
			if (self.item['id'] == item):
				for self.key in self.item:
					if (self.key == 'build'):
						self.buildable = True
						break
				break
		return(self.buildable)

	def ListAllItems(self, buildonly = True):
		self.itemlist = []
		del self.itemlist [:]
		for self.item in self.iteminfo:
			for self.key in self.item:
				if (self.key == 'build') and (buildonly):
					self.itemlist.append(self.item['id'])
					break
				else:
					self.itemlist.append(self.item['id'])
					break
		return(self.itemlist)


class ItemInventory:
	def __init__(self, knownItemList):
		self.itemsDict = collections.OrderedDict()
		self.updateItemList(knownItemList)

	def updateItemList(self, updatedItemList):
		self.found = False
		for self.listeditem in updatedItemList:
			for self.item in self.itemsDict:
				if (self.listeditem == self.item):
					self.found = True
					break
			if not (self.found):
				self.itemsDict[self.listeditem] = 0
			else:
				self.found = False

	def GetItems(self):
		return(self.itemsDict)

	def AddItems(self, item, number=1):
		self.itemsDict[item] += number

	def AddNewItem(self, item, number=0):
		self.itemsDict[item] = number

	def DeployRig(self):
		self.rigspare = False
		if (int(self.itemsDict['derrick']) > 0):
			self.itemsDict['derrick'] -= 1
			self.rigspare = True
		return(self.rigspare)

	def DeployItem(self, item, amount=1):
		self.success = False
		for self.item in self.itemsDict:
			if (self.item == item):
				if (self.itemsDict[item] >= amount):
					self.itemsDict[item] -= amount
					self.success = True
				break
		return(self.success)

	def RetrieveItem(self, item, amount=1):
		self.itemsDict[item] += amount


class RawMaterialsInventory:
	def __init__(self):	
		self.mineralsDict = collections.OrderedDict()
		self.mineralsDict['Iron'] = 0
		self.mineralsDict['Titanium'] = 0
		self.mineralsDict['Aluminium'] = 0
		self.mineralsDict['Carbon'] = 0
		self.mineralsDict['Copper'] = 0
		self.mineralsDict['Hydrogen'] = 0
		self.mineralsDict['Deuterium'] = 0
		self.mineralsDict['Methane'] = 0
		self.mineralsDict['Helium'] = 0
		self.mineralsDict['Palladium'] = 0
		self.mineralsDict['Platinum'] = 0
		self.mineralsDict['Silver'] = 0
		self.mineralsDict['Gold'] = 0
		self.mineralsDict['Silica'] = 0

		self.mineralAvailability = {
						'Iron': 4,
						'Titanium': 4, 
						'Aluminium': 4, 
						'Carbon': 2,
						'Copper': 4,
						'Hydrogen': 4,
						'Deuterium': 3,
						'Methane': 3,
						'Helium': 4,
						'Palladium': 1, 
						'Platinum': 1,
						'Silver': 1,
						'Gold': 1,
						'Silica': 1,
					}

	def InitInventory(self, Iron, Titanium, Aluminium, Carbon, Copper, Hydrogen, Deuterium, Methane, Helium, Palladium, Platinum, Silver, Gold, Silica):
		self.mineralsDict['Iron'] = int(Iron)
		self.mineralsDict['Titanium'] = int(Titanium)
		self.mineralsDict['Aluminium'] = int(Aluminium)
		self.mineralsDict['Carbon'] = int(Carbon)
		self.mineralsDict['Copper'] = int(Copper)
		self.mineralsDict['Hydrogen'] = int(Hydrogen)
		self.mineralsDict['Deuterium'] = int(Deuterium)
		self.mineralsDict['Methane'] = int(Methane)
		self.mineralsDict['Helium'] = int(Helium)
		self.mineralsDict['Palladium'] = int(Palladium)
		self.mineralsDict['Platinum'] = int(Platinum)
		self.mineralsDict['Silver'] = int(Silver)
		self.mineralsDict['Gold'] = int(Gold)
		self.mineralsDict['Silica'] = int(Silica)
	
	def GetMaterials(self):
		return(self.mineralsDict)

	def InitMining(self, MineralList):
		self.MineralSeam = collections.OrderedDict()
		for self.mineral in MineralList:
			self.MineralSeam[self.mineral] = 0

	def GetMineralSeam(self):
		return(self.MineralSeam)

	def DoMining(self, rigs):
		self.extraction = 2*rigs
		for self.mineral, self.amount in self.MineralSeam.items():
			if (self.amount == 0):
				if (random.randint(0, 8) == 7):
					self.rarity = self.mineralAvailability[self.mineral]
					self.MineralSeam[self.mineral] = random.randint(1000, 6001) * self.rarity
			else:
				if (self.mineralsDict[self.mineral] < MAXRESOURCES):
					self.amount -= self.extraction
					if (self.amount < 0):
						self.amount = 0
					self.MineralSeam[self.mineral] = self.amount
					self.mineralsDict[self.mineral] += self.extraction
				elif (self.mineralsDict[self.mineral] > MAXRESOURCES):
					self.mineralsDict[self.mineral] = MAXRESOURCES

	def removeMineral(self, resourcedict):
		for self.mineral in resourcedict:
			self.mineralsDict[self.mineral] -= resourcedict[self.mineral]

	def addMineral(self, resourcedict):
		for self.mineral in resourcedict:
			self.mineralsDict[self.mineral] += resourcedict[self.mineral]

	def addNewMineral(self, mineral, amount=0):
		self.mineralsDict[mineral] = int(amount)

	def CheckInventory(self, resourcedict):
		self.enough = True
		for self.mineral, self.amount in resourcedict.items():
			if (int(self.mineralsDict[self.mineral]) < int(self.amount)):
				self.enough = False
				break
		return(self.enough)

	def ShortageList(self, resourcedict):
		self.shortageList = []
		del self.shortageList [:]
		for self.mineral, self.amount in resourcedict.items():
			if (int(self.mineralsDict[self.mineral]) < int(self.amount)):
				self.shortageList.append(self.mineral)
		return(self.shortageList)

	def NumberCouldbeBuilt(self, resourcedict):
		self.BuildNumber = 0
		self.result = 0
		for self.mineral, self.amount in resourcedict.items():
			self.result = int(self.mineralsDict[self.mineral]) / int(self.amount)
			if (self.BuildNumber == 0):
				self.BuildNumber = self.result
			else:
				if (self.BuildNumber > self.result):
					self.BuildNumber = self.result
		return(self.BuildNumber)

	def MakeMeh(self):
		if (self.mineralsDict['Methane'] >= 2) and (self.mineralsDict['Hydrogen'] >= 2):
			self.mineralsDict['Methane'] -= 2
			self.mineralsDict['Hydrogen'] -= 2
			self.mineralsDict['MeH'] += 1

	def MakeHeD(self):
		if (self.mineralsDict['Helium'] >= 2) and (self.mineralsDict['Deuterium'] >= 2):
			self.mineralsDict['Helium'] -= 2
			self.mineralsDict['Deuterium'] -= 2
			self.mineralsDict['HeD'] += 1


class ShowItemButtons:
	def __init__(self, screen, scalegfx, purpose='build'):
		self.screen = screen
		self.scalegfx = scalegfx
		self.purpose = purpose
		self.interactiveIcons = []

		self.FlashingSelectDict = functions.scaled_image_dict('', '', '', 'gfx/FlashingItemButton.png', self.scalegfx, 0, 0)
		self.FlashingSelectAnimate = animate_this(self.screen, self.FlashingSelectDict['surface'], (self.FlashingSelectDict['rect'].width / 16), self.FlashingSelectDict['rect'].height, 0, 0, 100)

		if (purpose == 'research'):
			self.FlashingResearchDict = functions.scaled_image_dict('', '', '', 'gfx/FlashingResearchButton.png', self.scalegfx, 0, 0)
			self.FlashingResearchAnimate = animate_this(self.screen, self.FlashingResearchDict['surface'], (self.FlashingResearchDict['rect'].width / 16), self.FlashingResearchDict['rect'].height, 0, 0, 120)
		else:
			self.FlashingResearchAnimate = False

		if (purpose == 'build'):
			self.starty = 27
		else:
			self.starty = 36.5

	def display(self, iteminfo, itemlist, crewfound = False, itemselected = False, timepassed = 0):
		del self.interactiveIcons [:]
		self.itemnum = 0
		self.xoffset = 0
		self.yoffset = 0
		self.secondcol = False
		for self.item in itemlist:
			self.itemnum += 1
			if (self.itemnum > 16):
				if not (self.secondcol):
					self.secondcol = True
					self.yoffset = 0
					self.xoffset = 17
			self.newRect = pygame.Rect((281 + self.xoffset) * self.scalegfx, (self.starty * self.scalegfx + (9.5 * self.yoffset * self.scalegfx)), 3 * self.scalegfx, 3 * self.scalegfx)
			if (crewfound):
				if (iteminfo.OrbitOnly(self.item)):
					self.prodButtonColour = YELLOW
				else:
					self.prodButtonColour = GREEN
			else:
				self.prodButtonColour = RED
			self.interactiveIcons.append({'name': self.item,
						    	'description': iteminfo.GetItemDescription(self.item),
							'typeOfimage': self.purpose,
							'rect': pygame.draw.rect(self.screen, self.prodButtonColour, self.newRect, 0),
		      					})
			if (itemselected == self.item):
				self.FlashingSelectAnimate.setxy((281 + self.xoffset) * self.scalegfx, (self.starty * self.scalegfx + (9.5 * self.yoffset * self.scalegfx)))
				self.FlashingSelectAnimate.update(timepassed)
			self.yoffset += 1
		return(self.interactiveIcons)

	def displayResearch(self, iteminfo, researchDict, itemselected = False, timepassed = 0):
		del self.interactiveIcons [:]
		self.itemnum = 0
		self.xoffset = 0
		self.yoffset = 0
		self.secondcol = False
		self.typeOfImage = False
		for self.item in researchDict:
			self.itemnum += 1
			if (researchDict[self.item] == 'hidden'):
				self.yoffset += 1
				continue
			elif (researchDict[self.item] == 'done'):
				self.typeOfImage = self.purpose
			else:
				self.typeOfImage = 'startresearch'
			if (self.itemnum > 16):
				if not (self.secondcol):
					self.secondcol = True
					self.yoffset = 0
					self.xoffset = 17
			self.newRect = pygame.Rect((281 + self.xoffset) * self.scalegfx, (self.starty * self.scalegfx + (9.5 * self.yoffset * self.scalegfx)), 3 * self.scalegfx, 3 * self.scalegfx)
			if (researchDict[self.item] == 'done'):
				self.prodButtonColour = GREEN
			else:
				self.prodButtonColour = RED
			self.interactiveIcons.append({'name': self.item,
						    	'description': iteminfo.GetItemDescription(self.item),
							'typeOfimage': self.typeOfImage,
							'rect': pygame.draw.rect(self.screen, self.prodButtonColour, self.newRect, 0),
		      					})
			if (researchDict[self.item] == 'inprogress'):
				self.FlashingResearchAnimate.setxy((281 + self.xoffset) * self.scalegfx, (self.starty * self.scalegfx + (9.5 * self.yoffset * self.scalegfx)))
				self.FlashingResearchAnimate.update(timepassed)
			elif (itemselected == self.item):
				self.FlashingSelectAnimate.setxy((281 + self.xoffset) * self.scalegfx, (self.starty * self.scalegfx + (9.5 * self.yoffset * self.scalegfx)))
				self.FlashingSelectAnimate.update(timepassed)
			self.yoffset += 1
		return(self.interactiveIcons)


class Pod:
	def __init__(self, screen, scalegfx, podtype):
		self.screen = screen
		self.scalegfx = scalegfx
		self.podtype = podtype
		self.content = False
		self.amount = 0
		self.ToolPodTop = False
		self.baseframedelay = 50
		self.vframes = 48
		self.hframes = 48
		self.podFramesVertical = []
		self.podFramesHorizontal = []

		if (podtype == 'cryo'):
			self.PodDict = functions.scaled_image_dict('', '', '', 'gfx/pods/CryoPod.png', self.scalegfx, 0, 0)
		elif (podtype == 'supply'):
			self.PodDict = functions.scaled_image_dict('', '', '', 'gfx/pods/SupplyPod.png', self.scalegfx, 0, 0)
		else:
			self.PodDict = functions.scaled_image_dict('', '', '', 'gfx/pods/ToolPod.png', self.scalegfx, 0, 0)
			self.ToolPodTopDict = functions.scaled_image_dict('', '', '', 'gfx/pods/ToolPodTop.png', self.scalegfx, 0, 0)


class Shuttle:
	def __init__(self, screen, scalegfx, location, status):
		self.screen = screen
		self.scalegfx = scalegfx
		self.location = location
		self.status = status
		self.engine = False
		self.pod1 = False
		self.fuel = 0
		self.name = self.location + ' Shuttle'
		self.timepassed = 0
		self.baseframedelay = 50
		self.vframes = 48
		self.hframes = 48
		self.bayHlength = 256 * self.scalegfx
		self.chassisFramesVertical = []
		self.chassisFramesHorizontal = []
		self.shuttledeploy = False
		self.shuttleundeploy = False
		self.shuttlemoving = False
		self.interactiveIcons = []

		self.ShuttleChassisDict = functions.scaled_image_dict('', '', '', 'gfx/shuttle/ShuttleChassis.png', self.scalegfx, 0, 0)
		self.master_width, self.master_height = self.ShuttleChassisDict['surface'].get_size()

		for self.i in range(self.vframes):
			self.chassisFramesVertical.append(self.ShuttleChassisDict['surface'].subsurface((0, 0, self.bayHlength, (self.master_height / self.vframes) * self.i )))
		self.shuttleup_max = len(self.chassisFramesVertical)-1
		self.shuttleup_pos = 0
		self.upx = 64 * self.scalegfx
		self.upy = 202 * self.scalegfx
		self.upframedelay = self.baseframedelay

		for self.i in range(0, 32):
			if (self.i == 0):
				self.movingX = 0
			else:
				self.movingX += (self.master_width / self.hframes)
			self.chassisFramesHorizontal.append(self.ShuttleChassisDict['surface'].subsurface(self.movingX, 0, self.bayHlength, self.master_height))
		self.shuttlehorizontal_max = len(self.chassisFramesHorizontal)-1
		self.shuttleh_pos = 0
		self.hx = 64 * self.scalegfx
		self.hy = 84 * self.scalegfx
		self.hframedelay = 30

	def NewShuttle(self, timedelay):
		self.timepassed += timedelay
		if (int(self.timepassed) >= int(self.upframedelay)):
			self.timepassed = self.timepassed - self.upframedelay
			if (self.timepassed < 0):
				self.timepassed = 0
			if self.shuttleup_pos < self.shuttleup_max:
				self.shuttleup_pos += 1
				self.upy -= self.master_height / self.vframes
				self.upframedelay += 0.5
		if self.shuttleup_pos < self.shuttleup_max:
			self.shuttledeploy = True
			self.tempSurf = self.chassisFramesVertical[self.shuttleup_pos]
			self.screen.blit(self.tempSurf,(self.upx, self.upy))
		else:
			self.shuttledeploy = False

	def ShuttleDeploying(self):
		return(self.shuttledeploy)

	def ShuttleUnDeploying(self):
		return(self.shuttleundeploy)

	def ShuttleMoving(self):
		return(self.shuttlemoving)

	def RemoveShuttle(self, timedelay):
		self.timepassed += timedelay
		if (int(self.timepassed) >= int(self.upframedelay)):
			self.timepassed = self.timepassed - self.upframedelay
			if (self.timepassed < 0):
				self.timepassed = 0
			if self.shuttleup_pos > 0:
				self.shuttleundeploy = True
				self.shuttleup_pos -= 1
				self.upy += self.master_height / self.vframes
				self.upframedelay -= 0.5
		if self.shuttleup_pos > 0:
			self.tempSurf = self.chassisFramesVertical[self.shuttleup_pos]
			self.screen.blit(self.tempSurf,(self.upx, self.upy))
		else:
			self.shuttleundeploy = False

	def ShuttleManage(self, timedelay, manage = 'cockpit'):
		del self.interactiveIcons [:]
		if (manage == 'cockpit'):
			self.DisplayShuttle(timedelay, 0)
		elif (manage == 'pod1'):
			self.DisplayShuttle(timedelay, 14)
		elif (manage == 'drive'):
			self.DisplayShuttle(timedelay, 30)
		return(self.interactiveIcons)

	def DisplayShuttle(self, timedelay, framepos):
		self.timepassed += timedelay
		if (int(self.timepassed) >= int(self.hframedelay)):
			self.timepassed = self.timepassed - self.upframedelay
			if (self.timepassed < 0):
				self.timepassed = 0
			if not (self.shuttleh_pos == framepos):
				self.shuttlemoving = True
				if (self.shuttleh_pos < framepos):
					self.shuttleh_pos += 1
				elif (self.shuttleh_pos > framepos):
					self.shuttleh_pos -= 1
			else:
				self.shuttlemoving = False
		self.tempSurf = self.chassisFramesHorizontal[self.shuttleh_pos]
		self.screen.blit(self.tempSurf,(self.hx, self.hy))







